namespace Vanilla.Tools.BuiltIn

import Vanilla
import Vanilla.UI

class BuiltInTool (UI.Tool, ILoadBitmap, ISaveBitmap, IGenerateThumbnail):
	ToolDir as File

	def constructor (f as File):
		ToolDir = f
	
	// Used to load a PNG file
	def LoadBitmap (f as File) as BitmapCanvas:
		print "LoadBitmap ${f.Path}"
		return BitmapCanvas(f.Path)
	
	// Used to save a PNG file
	def SaveBitmap (f as File, b as BitmapCanvas):
		print "SaveBitmap ${f.Path}"
		b.WriteToPNG(f.Path)
	
	// Used to generate a thumbnail for a bitmap or tool
	def GenerateThumbnail (f as File, s as Size) as BitmapCanvas:
		if f.Type.IsAlso("Tool"):
			if File(f.Path + "/Tool.png").HasData:
				return GenerateThumbnail(File(f.Path + "/Tool.png"), s)
			else:
				return GenerateThumbnail(File(ToolDir.Path + "/DefaultToolIcon.png"), s)
		elif f.Type.IsAlso("Bitmap"):
			try:
				using image = BitmapCanvas.Load(f):
					thumb = BitmapCanvas(s.Width, s.Height)
					using brush = Brush(thumb):
						brush.Scale(s.Width / image.Size.Width, s.Height / image.Size.Height)
						using brush.Pattern = CanvasPattern(image):
							brush.Paint()
					return thumb
			except e as NoActionException:
				thumb = BitmapCanvas(s.Width, s.Height)
				using brush = Brush(thumb):
					brush.Draw(Rect(thumb.Size))
					brush.LineWidth = 2
					brush.Colour = Colour.Red.Alpha(0.25)
					brush.FillPreserve()
					brush.Colour = Colour.Grey
					brush.Stroke()
				return thumb
		else:
			return GenerateThumbnail(File(ToolDir.Path + "/DefaultFileIcon.png"), s)
